<?php

namespace App\Console\Commands\Command;

use App\Models\Common\Area\Repositories\Interfaces\AreaRepositoryInterface;
use Exception;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Facades\Excel;

class UpdateAreaTranslationBy
{
    /**
     * @throws Exception
     */
    public static function byExcel(): void
    {
        $excel = storage_path('translation/area.xlsx');
        if (!file_exists($excel)) {
            throw new Exception('翻译文件不存在');
        }
        $collection = new Collection();
        $data       = Excel::toArray($collection, $excel);
        $sheetData  = $data[0];
        array_shift($sheetData);
        $areaRepo = app(AreaRepositoryInterface::class);
        foreach ($sheetData as $value) {
            $area = $areaRepo->findByCode($value['3']);
            if ($area) {
                $areaRepo->updateArea($area, [
                    'name_uy'   => $value['4'],
                    'letter_uy' => $value['5'],
                ]);
            }
        }

    }
}